<template>
  <!-- 添加或修改请假申请对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="80vw" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <form-block-title title="基础信息"/>
      <el-row>
        <el-col :span="10">
          <el-form-item label="请假类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择请假类型" style="width:100%;">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        </el-col>
        <el-col :span="20">
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        </el-col>
        <el-col :span="10">
        <el-form-item label="开始时间" prop="leaveStartTime">
          <el-date-picker clearable style="width: 100%;"
                          v-model="form.leaveStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        </el-col>
        <el-col :span="10">
        <el-form-item label="结束时间" prop="leaveEndTime">
          <el-date-picker clearable style="width: 100%;"
                          v-model="form.leaveEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        </el-col>
        <el-col :span="20">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="submitForm" type="primary">保 存</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getLeaveApplication, addLeaveApplication, updateLeaveApplication, } from '@/api/example/leaveappr/base'
  import FormBlockTitle from '@/components/ScFormBlockTitle/index'

  export default {
    name: 'Edit',
    components: { FormBlockTitle },
    data() {
      return {
        id: -1,
        // 弹出层标题
        title: '录入信息',
        // 是否显示弹出层
        open: false,
        // 请假类型字典
        typeOptions: [],
        // 表单参数
        form: {}
        ,
        // 表单校验
        rules: {}
      }

    },
    created() {
      this.getDicts('sys_notice_type').then(response => {
        this.typeOptions = response.data
      })
    },
    methods: {
      init(id) {
        //编辑页面加载数据
        if (id && id !== -1) {
          this.id = id
          getLeaveApplication(id).then(response => {
            this.form = response.data
          })
        }
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          type: null,
          title: null,
          reason: null,
          leaveStartTime: null,
          leaveEndTime: null,
          instanceId: null,
          state: null,
          aclDeptId: null,
          aclCreateById: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }

        this.resetForm('form')
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateLeaveApplication(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              addLeaveApplication(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.$emit('refreshDataList')
                }
              })
            }
          }
        })
      }
    }
  }
</script>
